package com.base.pig.controller;


import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.pig.entity.ScDeviceType;
import com.base.pig.service.ScDeviceTypeService;
import com.base.pig.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 设备类型表
 * </p>
 *
 * @author mag
 * @since 2022-04-21
 */
@RestController
@Api(value = "monitor", tags = "设备类型")
@RequestMapping("/devicetype")
public class ScDeviceTypeController {

    @Autowired
    public ScDeviceTypeService scDeviceTypeService;

    @ApiOperation(value = "条件查询(type_code,type_name)")
    @PostMapping("/list")
    public R list(@RequestBody ScDeviceType scDeviceType){

        QueryWrapper<ScDeviceType> wrapper = new QueryWrapper<ScDeviceType>();

        wrapper.eq(!StringUtil.isNullOrEmpty(scDeviceType.getTypeCode()), "type_code", scDeviceType.getTypeCode());
        wrapper.eq(!StringUtil.isNullOrEmpty(scDeviceType.getTypeName()), "type_name", scDeviceType.getTypeName());

        List<ScDeviceType> scDeviceTypeList = scDeviceTypeService.list(wrapper);
        return R.ok(scDeviceTypeList,"查询成功");
    }

    @ApiOperation(value = "分页查询(type_code,type_name)")
    @PostMapping("/pageList/{pageNum}/{pageSize}")
    public R pageList(@PathVariable("pageNum")Long pageNum,
                  @PathVariable("pageSize")Long pageSize,
                  @RequestBody ScDeviceType scDeviceType){

        QueryWrapper<ScDeviceType> wrapper = new QueryWrapper<ScDeviceType>();

        wrapper.eq(!StringUtil.isNullOrEmpty(scDeviceType.getTypeCode()), "type_code", scDeviceType.getTypeCode());
        wrapper.eq(!StringUtil.isNullOrEmpty(scDeviceType.getTypeName()), "type_name", scDeviceType.getTypeName());

        IPage<ScDeviceType> page = scDeviceTypeService.page(
        new Page<>(pageNum, pageSize),
                wrapper);

        return R.ok(page,"分页查询成功");
    }

    @ApiOperation(value = "根据id查询详情")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") Long id){
        ScDeviceType scDeviceType = scDeviceTypeService.getById(id);
        return R.ok(scDeviceType,"查询成功");
    }

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public R save(@RequestBody ScDeviceType scDeviceType){

        //设备类型编码和设备类型名称不能为空
        if (!StringUtil.isNullOrEmpty(scDeviceType.getTypeCode()) && !StringUtil.isNullOrEmpty(scDeviceType.getTypeName()))
        {
            //设备类型编码和设备类型名称唯一
            List<ScDeviceType> typeList = scDeviceTypeService.list(Wrappers.<ScDeviceType>query().lambda()
                    .eq(ScDeviceType::getTypeCode, scDeviceType.getTypeCode())
                    .or()
                    .eq(ScDeviceType::getTypeName, scDeviceType.getTypeName()));

            if(typeList!=null &&   typeList.size()>0)
            {
                return R.failed(null, "类型编码或者类型名称不能重复)");
            }else{

                boolean b = scDeviceTypeService.save(scDeviceType);
                if (b){
                    return R.ok(b,"添加成功");
                }
                return R.failed(null,"添加失败");
            }

        }else{
            return R.failed(null, "类型编码或者类型名称不能为空)");
        }

    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        scDeviceTypeService.removeById(id);

        return R.ok(null,"删除成功");
    }

    @ApiOperation(value = "根据id修改")
    @PostMapping("/update/{id}")
    public R update(@RequestBody ScDeviceType scDeviceType){

        //设备类型编码,设备类型名称,id不能为空
        if(scDeviceType.getId()==null)
        {
            return R.failed(null,"未找到该数据");
        }

        if (!StringUtil.isNullOrEmpty(scDeviceType.getTypeCode()) && !StringUtil.isNullOrEmpty(scDeviceType.getTypeName())) {
            //设备类型编码和设备类型名称唯一,(type_code=? or  type_name=?) and  id<>?
            LambdaQueryWrapper<ScDeviceType> queryWrapper = new QueryWrapper<ScDeviceType>().lambda();
            queryWrapper.ne(ScDeviceType::getId, scDeviceType.getId())
                    .and(wrapper -> wrapper.eq(ScDeviceType::getTypeCode, scDeviceType.getTypeCode())
                                           .or()
                                           .eq(ScDeviceType::getTypeName, scDeviceType.getTypeName()));


            List<ScDeviceType> typeList = scDeviceTypeService.list(queryWrapper);

            if(typeList!=null &&   typeList.size()>0)
            {
                return R.failed(null, "类型编码或者类型名称重复)");
            }else{
                return R.ok( scDeviceTypeService.updateById(scDeviceType),"修改成功");
            }


        }else{
            return R.failed(null, "类型编码或者类型名称不能为空)");
        }

    }


}
